﻿using AutoMapper;
using B.S.Backtrack.Domain.Entities;
using B.S.Backtrack.ErrorCode;
using B.S.Backtrack.Infrastructure.Interface;
using B.S.Backtrack.Read.API.Application.Command.Acc;
using MediatR;

namespace B.S.Backtrack.Read.API.Application.CommandHandler.Acc
{
    public class LoginAccountHandler : IRequestHandler<LoginAccountCommand, ApiResult<Account>>
    {
        private readonly IBaseRepository<Account> accRep;
        private readonly IMapper mapper;

        public LoginAccountHandler(IBaseRepository<Account> accRep,IMapper mapper)
        {
            this.accRep = accRep;
            this.mapper = mapper;
        }
        public Task<ApiResult<Account>> Handle(LoginAccountCommand request, CancellationToken cancellationToken)
        {
            ApiResult<Account> result = new ApiResult<Account>();
            var user = accRep.GetAll().Where(x => x.AccountName == request.AccountName).FirstOrDefault();
            if (user == null)
            {
                result.Code = ApiEnums.查询失败;
                result.Msg = ApiEnums.查询失败.ToString();

            }
            else
            {
                if (user.AccountPwd != request.AccountPwd)
                {
                    result.Code = ApiEnums.查询失败;
                    result.Msg = ApiEnums.查询失败.ToString();
                }
                else
                {
                    result.Code = ApiEnums.查询成功;
                    result.Msg = ApiEnums.查询成功.ToString();
                    result.Data = user;

                }
            }

            return Task.FromResult(result);
        }
    }
}
